#!/bin/bash

#======================================================
#   System Required:  Debian 10+ 
#   Description: Manage clash
#   Author: J. 2021.12.20
#======================================================

red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
apt install curl wget lsof -y
url_cdn="https://ghproxy.20120714.xyz"
echo "199.232.68.133 raw.githubusercontent.com" >> /etc/hosts
webget(){
	#参数【$1】代表下载目录，【$2】代表在线地址
	#参数【$3】代表输出显示，【$4】不启用重定向
	if curl --version > /dev/null 2>&1;then
		[ "$3" = "echooff" ] && progress='-s' || progress='-#'
		[ -z "$4" ] && redirect='-L' || redirect=''
		result=$(curl -w %{http_code} --connect-timeout 5 $progress $redirect -ko $1 $2)
	else
		if wget --version > /dev/null 2>&1;then
			[ "$3" = "echooff" ] && progress='-q' || progress='-q --show-progress'
			[ "$4" = "rediroff" ] && redirect='--max-redirect=0' || redirect=''
			certificate='--no-check-certificate'
			timeout='--timeout=3'
		fi
		[ "$3" = "echoon" ] && progress=''
		[ "$3" = "echooff" ] && progress='-q'
		wget $progress $redirect $certificate $timeout -O $1 $2 
		[ $? -eq 0 ] && result="200"
	fi
}

webget /srv/version $url_cdn/https://raw.githubusercontent.com/07031218/c-m/main/version >> /dev/null 2>&1
webget /usr/bin/clash_ui $url_cdn/https://raw.githubusercontent.com/07031218/c-m/main/clash_ui
#wget -O /usr/bin/clash_ui https://raw.githubusercontent.com/07031218/c-m/main/clash_ui >> /dev/null 2>&1
chmod +x /usr/bin/clash_ui >> /dev/null 2>&1
echo "---------------------------------------"
echo -e "${green}Clash_ui 安装成功${plain}"
echo -e "${green}Clash_UI使用方法 clash_ui${plain}"
echo -e "${green}第一次请用命令安装 clash_ui install${plain}"
echo "---------------------------------------"
clash_ui install
